package com.helasasa.maxfunds.lineofcredit.cashloan.ghana.screen.proof.wallet.subview

import android.graphics.Color
import android.graphics.Typeface
import android.os.Bundle
import android.text.Spannable
import android.text.SpannableString
import android.text.Spanned
import android.text.style.ForegroundColorSpan
import android.text.style.RelativeSizeSpan
import android.text.style.StyleSpan
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.Window
import android.widget.ImageView
import android.widget.TextView
import androidx.fragment.app.DialogFragment
import com.helasasa.maxfunds.lineofcredit.cashloan.R
import org.json.JSONObject

interface IProofGhanaWalletEmptyDialogListener {
    fun onClick(type: Int)
}

class ProofGhanaWalletEmptyDialog(var onItemClick: IProofGhanaWalletEmptyDialogListener) :
    DialogFragment() {

    private lateinit var title: TextView
    private lateinit var confirm: TextView
    private lateinit var cancel: TextView
    private lateinit var close: ImageView


    private var walletInfo: JSONObject? = null

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?,
    ): View {
        dialog?.requestWindowFeature(Window.FEATURE_NO_TITLE)
        return inflater.inflate(R.layout.view_ghana_proof_wallet_empty_dialog, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        dialog?.setCancelable(true)
        dialog?.setCanceledOnTouchOutside(true)

        title = view.findViewById(R.id.title)
        confirm = view.findViewById(R.id.confirm)
        cancel = view.findViewById(R.id.cancel)
        close = view.findViewById(R.id.close)

        initializeView()
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setStyle(STYLE_NO_TITLE, R.style.Dialog)
    }

    override fun onStart() {
        super.onStart()

        dialog?.window?.let {
            it.attributes.gravity = Gravity.CENTER
            it.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
        }
        updateSpanText()
    }

    private fun initializeView() {
        confirm.setOnClickListener {
            onItemClick.onClick(1)
            dismiss()
        }

        cancel.setOnClickListener {
            onItemClick.onClick(0)
            dismiss()
        }

        close.setOnClickListener {
            dismiss()
        }
    }

    private fun updateSpanText() {
        val text = title.text.toString()
        val words = "2 comptes"
        val gStart = text.indexOf(words)
        if (gStart >= 0) {
            val gEnd = gStart + words.length
            val spanString = SpannableString(text)
            spanString.setSpan(
                RelativeSizeSpan(1.2F),
                gStart,
                gEnd,
                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
            )
            spanString.setSpan(
                ForegroundColorSpan(Color.parseColor("#A4690A")),
                gStart,
                gEnd,
                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
            )
            spanString.setSpan(
                StyleSpan(Typeface.BOLD), gStart, gEnd,
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
            )

            title.text = spanString
        }
    }

}